package com.mago.tis.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.mago.tis.dao.ICompanyDao;
import com.mago.tis.pojo.Tiscit;
import com.mago.tis.pojo.Tistit;
import com.mago.tis.utility.ApplicationException;
import com.mago.tis.utility.ExceptionMessage;
import com.mago.tis.utility.QueryConstant;

public class CompanyDao extends BaseDao implements ICompanyDao{

	public boolean updateCompanyInfo(Tiscit tiscit) throws ApplicationException {
		// Prepare the parameters
		List lParamList = new ArrayList();
		boolean isupdated = false;
		if (tiscit != null) {
			try {
				lParamList.add(0, "" + tiscit.getCompanyId());
				lParamList.add(1, tiscit.getCompanyName());
				lParamList.add(2, tiscit.getAddress());
				
				// Get the query by passing parameters
				String query = getQuery(QueryConstant.UPDATE_COMPANY, lParamList);
				int resultCount = insert(query);
				if (resultCount != 0) {
					isupdated = true;
				} else
					isupdated = false;
				closeConnection();

			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.COMPANY_NOT_SAVED);
			}
		}
		return isupdated;

	}

	@Override
	public List<Tiscit> getCompanyOnId(String companyId)
			throws ApplicationException {
		List<Tiscit> companyInfo = new ArrayList<Tiscit>();
		List lParamList = new ArrayList();
		lParamList.add(0, companyId);

		String query = getQuery(QueryConstant.GET_COMPANY_ON_ID, lParamList);
		try {
			ResultSet rs = getResults(query);
			while (rs.next()) {
				Tiscit cit = new Tiscit();
				cit.setCompanyId(rs.getInt("COMPANY_ID"));
				cit.setCompanyName(rs.getString("COMPANY_NAME"));
				cit.setAddress(rs.getString("COMPANY_ADDRESS"));
				cit.setAbout(rs.getString("ABOUT"));
				companyInfo.add(cit);
			} 
		}
		catch (SQLException sqe) {
			throw new ApplicationException(
					ExceptionMessage.COMPANY_NOT_SAVED);
		}
		return companyInfo;
	}

	@Override
	public boolean updateAboutInfo(Tiscit tiscit) throws ApplicationException {
		List lParamList = new ArrayList();
		boolean isupdated = false;
		if (tiscit != null) {
			try {
				lParamList.add(0, "" + tiscit.getCompanyId());
				lParamList.add(1, tiscit.getAbout());
				
				// Get the query by passing parameters
				String query = getQuery(QueryConstant.UPDATE_ABOUT, lParamList);
				int resultCount = insert(query);
				if (resultCount != 0) {
					isupdated = true;
				} else
					isupdated = false;
				closeConnection();

			} catch (SQLException sqe) {
				throw new ApplicationException(
						ExceptionMessage.COMPANY_NOT_SAVED);
			}
		}
		return isupdated;

	}

}
